__author__ = 'Soul'

from core.JsonDataFetcher import *

class ChinaCarJsonFetcher(JsonDataFetcher):
    def __init__(self):
        self._jsondata_ = ''
        self.__db_key__ = "chinacardb"
        self.__db_type__ = "sqlite"
        self.__t_current_page__ = 1

    def init(self, url, intent=False, recurse=False):
        if isinstance(intent, dict):
            try:
                self.__t_current_page__ = intent["page"]
            except:
                pass
        return JsonDataFetcher.init(self, url, recurse)

    def preFectch(self, params, headers):
        import time
        self.__url__ = self.__url__ + "?_dc=" + str(int(time.time()))

        if params == None: params = {}
        for i in range(0, 30):
            k = 's%s' % str(i)
            params[k] = ""
        params['s10'] = '%u7535'
        for i in range(1, 10):
            k1 = 's_%s' % str(i)
            k2 = 'ss_%s' % str(i)
            params[k1] = ""
            params[k2] = 1

        self.__t_current_limit__ = 400
        params['page'] = self.__t_current_page__
        params['limit'] = self.__t_current_limit__
        params['start'] = (self.__t_current_page__ - 1) * \
                          self.__t_current_limit__

        if headers == None: headers = {}
        headers['host'] = 'www.chinacar.com.cn'
        headers['Connection'] = 'keep-alive'
        headers['Accept'] = 'application/x-json;text/x-json;charset=GB2312'
        headers['Origin'] = 'http://www.chinacar.com.cn'
        headers['X-Requested-With'] = 'XMLHttpRequest'
        headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
        headers['Referer'] = 'http://www.chinacar.com.cn/ggcx_new/list.asp'
        headers['Accept-Encoding'] = 'gzip, deflate'
        headers['Accept-Language'] = 'en-US,en;q=0.8,zh-CN;q=0.6,zh-TW;q=0.4'
        #headers['Cookie'] = "ASPSESSIONIDASQRBASQ=OBGBIEJCANDCHJOFFEACDIJL;"

    def preProcess(self): pass

    '''
        url: http://www.chinacar.com.cn/search.html
        The json data here should be parsed!
    '''
    def analysis(self):
        total_count = 0
        try:
           total_count = (int)(self.__json_data__['totalCount'])
        except Exception as err:
            print err

        if total_count <= 0: return

        for topic in self.__json_data__['topics']:
            from dboperater.DbOperatorFactory import *
            factory = DbOperatorFactory()
            operator = factory.GetDbOperator(self.__db_key__)
            last_id = operator.insert_record(topic)
            '''
                Should start a new fetcher for a detail fetch, pass the last_id to the child fetch
            '''
            tarid = topic['tarid']
            url = "http://www.chinacar.com.cn/ggcx_new/search_param.asp?id=" + tarid
            urls_to_visit.append({"url": url, "controller": "ChinaCarPageFetcher"})


        if self.__t_current_page__ * self.__t_current_limit__ < total_count:
            '''
            if we not finish loading all the data, continue
            '''
            urls_to_visit.append({"url": "http://www.chinacar.com.cn/ggcx_new/search_json.asp",
                                  "controller": "ChinaCarJsonFetcher",
                                  "intent": {
                                      "page": self.__t_current_page__ + 1
                                  }})